<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<style>
    .table-border tbody tr td{
        border-top: 1px solid black;
    }
</style>
<!--更换责任人模态框-->
<div class="modal fade" id="modal-change">
    <div class="modal-dialog" style="width:350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">更换创建人</h4>
            </div>
            <form id="edit-parameter-form" class="form-horizontal">
                <div class="modal-header">
                    <div class="col-md-12 row" style="font-weight: bold">
                        <div class="form-inline">
                            <div class="form-group">
                                <label class="control-label">
                                    原创建人：
                                </label>
                                <label class="control-label">
                                    <span id="currentFounder">

                                    </span>
                                </label>
                            </div>
                        </div>
                        <div class="form-inline">
                            <div class="form-group">
                                <label class="control-label">
                                    新创建人：
                                </label>
                                <label class="control-label">
                                    <input id="batId" type="hide" style="display:none" />
                                    <select id="newFounder" class="form-control">

                                    </select>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-info file-contract btn-file"
                            id="btn-save-change-founder">保存
                    </button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!--密码确认模态框-->
<div class="modal fade" id="modal-pass">
    <div class="modal-dialog" style="width:350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">请输入密码验证是本人操作</h4>
            </div>
            <form id="pass-form" class="form-horizontal">
                <div class="modal-header">
                    <div class="col-md-12 row" style="font-weight: bold">
                        <div class="form-inline">
                            <div class="form-group">
                                <label class="control-label">
                                    密码：
                                </label>
                                <label class="control-label">
                                    <input name="password" type="password" />
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-info file-contract btn-file"
                            id="btn-verification" data="">验证
                    </button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>项目核算列表</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box" style="padding-left:0px;">
                <div class="box-header">
                    <form id="queryForm" class="form-horizontal" style="margin-left:0px;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        项目编号：
                                    </label>
                                    <label class="control-label">
                                        <input name="code" class="form-control" type="text" maxlength="255" value=""
                                               placeholder="请输入项目编号"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        项目名称：
                                    </label>
                                    <label class="control-label">
                                        <input name="name" class="form-control" type="text" maxlength="255"
                                               placeholder="请输入项目名称"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        需方：
                                    </label>
                                    <label class="control-label">
                                        <input name="demander" class="form-control" type="text" maxlength="255"
                                               placeholder="请输入需方"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        安装地点：
                                    </label>
                                    <label class="control-label">
                                        <input name="address" class="form-control" type="text" maxlength="255"
                                               placeholder="请输入安装地点"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        负责人：
                                    </label>
                                    <label class="control-label">
                                        <input name="appoint" class="form-control" type="text" maxlength="255"
                                               placeholder="请输入负责人"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        创建者：
                                    </label>
                                    <label class="control-label">
                                        <input name="userName" class="form-control" type="text" maxlength="255"
                                               placeholder="请输入创建者"/>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        设计类别：
                                    </label>
                                    <label class="control-label">
                                        <select class="form-control" name="projectClassify">
                                            <option value="">请选择</option>
                                            <option value="1">设计</option>
                                            <option value="2">非设计</option>
                                        </select>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        任务状态：
                                    </label>
                                    <label class="control-label">
                                        <select class="form-control" name="tstatus">
                                            <option value="" selected="selected">请选择</option>
                                            <option value="1">未开始</option>
                                            <option value="2">执行中</option>
                                        </select>
                                    </label>
                                </div>
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">
                                        是否创建清单：
                                    </label>
                                    <label class="control-label">
                                        <select class="form-control" name="isCreateSheet">
                                            <option value="" selected="selected">请选择</option>
                                            <option value="1">创建</option>
                                            <option value="0">未创建</option>
                                        </select>
                                    </label>
                                </div>
<!--                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">-->
<!--                                    <label class="control-label">-->
<!--                                        工作组：-->
<!--                                    </label>-->
<!--                                    <label class="control-label">-->
<!--                                        <select class="form-control selectpicker" name="groupNameStr"-->
<!--                                                data-actions-box="true" multiple onchange="changeGroupNameStrVal(this)">-->
<!--                                            <option selected="selected" style="width: 200px;text-align: center"-->
<!--                                                    th:each="item:${groupModelList}" th:value="${item.id}"-->
<!--                                                    th:text="${item.groupName}"></option>-->
<!--                                        </select>-->
<!--                                        <input name="groupNameStr" type="hide" style="display: none" value=""/>-->
<!--                                    </label>-->
<!--                                </div>-->
                                <div class="form-group margin-bottom-none" style="margin-left:10px;margin-right: 25px;">
                                    <label class="control-label">任务下达时间：</label>
                                    <label class="control-label">
                                        <input type="text" class="form-control datepicker" name="startTime"
                                               readonly="readonly"/>
                                        <span> 至 </span>
                                        <input type="text" class="form-control datepicker" name="endTime"
                                               readonly="readonly"/></label>
                                </div>
                                <button id="btn-search" type="button" class="btn btn-success pull-right">
                                    搜索
                                </button>
                            </div>
                        </div>

                    </form>
                </div>
                <div id="toolbar" class="row">
                    <!--                    <button id="btn_getData" class="btn btn-success">获取数据</button>-->
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"  class="table-border" ></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table;

    $(function () {
        /**
         * 加载列表
         */
        table = js.table.init({
            url: ctx + "accountManage/list",
            showColumns: true,
            showExport: true,
            columns: [
                {
                    title: '序号', field: 'id', width: '60',
                    formatter: function (value, row, index, field) {
                        var pageSize = table.bootstrapTable('getOptions').pageSize;
                        var pageNumber = table.bootstrapTable('getOptions').pageNumber;
                        return pageSize * (pageNumber - 1) + index + 1;
                    }
                },
                {
                    title: '项目编号', field: 'code',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '项目名称', field: 'name',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '需方', field: 'demander',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }

                    }
                },
                {
                    title: '安装地点', field: 'address',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '项目数量', field: 'num',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '设计类别', field: 'projectClassify',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return "设计";
                        } else if (value === 2) {
                            return "非设计";
                        }
                    }
                },
                // {
                //     title: '工作组', field: "groupName",
                //     formatter: function (value, row, index) {
                //         if (value != null) {
                //             return value;
                //         } else {
                //             return '--';
                //         }
                //     }
                // },
                {
                    title: '负责人', field: "appoint",
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '创建者', field: "userName",
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: '任务下达时间', field: "createTime",
                    formatter: function (value, row, index) {
                        return value;
                    }
                },
                {
                    title: '任务状态', field: 'tstatus',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return '<lable class="badge label-default">未开始</lable>';
                        } else if (value === 2) {
                            return '<lable class="badge label-warning">执行中</lable>';
                        }
                    }
                },
                {
                    title: '操作', width: '180',
                    formatter: function (value, row, index) {
                        let elements = new Array();
                        if (row.adtId != null) {
                            if (hasP('ver:collec:change')) {
                                elements.push('<button class="btn btn-facebook btn-xs" onclick="change(' + row.adtId + ',\''+row.userName+'\','+row.groupId+')">更换创建人</button>');
                            }
                        }
                        if (row.adtId === null) {
                            if (hasP('ver:list:view')) {
                                elements.push('<button class="btn btn-success btn-xs" onclick="createCostSheet(\'' + row.code + '\')">创建成本清单</button>');
                            }
                        }
                        if (row.userName != null && row.userName === sessionUser.name || sessionUser.name === '管理员') {

                            if (row.isSave === 0) {
                                if (hasP('ver:collec:revoke')) {
                                    elements.push('<button class="btn btn-warning btn-xs" onclick="revoke(' + row.adtId + ')">撤销创建</button>');
                                }
                                if (hasP('ver:collec:revoke')) {
                                    elements.push('<button class="btn btn-danger btn-xs" onclick="restTask(' + row.adtId + ')">重置并撤销</button>');
                                }
                                if (hasP('ver:collec:view')) {
                                    elements.push('<button class="btn btn-info btn-xs" onclick="gotoGetData(' + row.adtId + ',\'' + row.code + '\')">数据获取</button>');
                                }
                                if (hasP('ver:adjust:view')) {
                                    elements.push('<button class="btn btn-info btn-xs" onclick="gotoAccounting(' + row.adtId + ',\'' + row.code + '\')">成本核算</button>');
                                }
                            }
                            if (row.isSave === 1) {
                                if (hasP('ver:adjust:view')) {
                                    elements.push('<button class="btn btn-warning btn-xs" onclick="cancelSave(' + row.adtId + ')">取消保存</button>');
                                }
                            }
                        }
                        // if (row.isSameDept === 1) {
                        //
                        //     // if (row.userName != null && row.userName === sessionUser.name || sessionUser.name === '管理员') {
                        //     //     if (row.isSave === 0) {
                        //     //         if (hasP('ver:collec:revoke')) {
                        //     //             elements.push('<button class="btn btn-warning btn-xs" onclick="revoke(' + row.adtId + ')">撤销创建</button>');
                        //     //         }
                        //     //         if (hasP('ver:collec:revoke')) {
                        //     //             elements.push('<button class="btn btn-danger btn-xs" onclick="restTask(' + row.adtId + ')">重置并撤销</button>');
                        //     //         }
                        //     //         if (hasP('ver:collec:view')) {
                        //     //             elements.push('<button class="btn btn-info btn-xs" onclick="gotoGetData(' + row.adtId + ',\'' + row.code + '\')">数据获取</button>');
                        //     //         }
                        //     //         if (hasP('ver:adjust:view')) {
                        //     //             elements.push('<button class="btn btn-info btn-xs" onclick="gotoAccounting(' + row.adtId + ',\'' + row.code + '\')">成本核算</button>');
                        //     //         }
                        //     //     }
                        //     //     if (row.isSave === 1) {
                        //     //         if (hasP('ver:adjust:view')) {
                        //     //             elements.push('<button class="btn btn-warning btn-xs" onclick="cancelSave(' + row.adtId + ')">取消保存</button>');
                        //     //         }
                        //     //     }
                        //     // }
                        // }
                        return elements.join('');
                    }
                }

            ]
        });
        /**
         * 表格搜索
         */
        $("#btn-search").click(function () {
            js.table.search(table);
        });
        /**
         * 模态框隐藏事件， 重置表单，重置验证
         */
        js.modal.hideEvent("modal-edit", function () {
            js.validate.reset("halfAdjEditForm");
            js.reset("halfAdjEditForm");
        });
        /**
         * 更换创建人
         */
        $("#btn-save-change-founder").on('click',function(e) {
           let newFounder = $("#newFounder").val();
           if(newFounder === null || newFounder === ''){
               js.modal.warning("请选择创建人！");
               return;
           }else{
               js.post({
                   url: ctx + 'getData/changeFounder',
                   type: 'post',
                   traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                   dataType: 'json',
                   contentType: "application/x-www-form-urlencoded",
                   sync:false,
                   data: {
                       id: $("#batId").val(),
                       taskUid:newFounder
                   },
                   async: true,                // 异步执行
                   success: function (result) {
                       js.modal.success(result.msg);
                       js.table.refresh(table);
                   },
                   error: function () {
                       js.modal.fail();
                   }
               });
           }
        });
        /**
         * 验证按钮
         */
        $("#btn-verification").on('click',function(e) {
            js.post({
                url: ctx + 'getData/isPass',
                type: 'post',
                traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                dataType: 'json',
                contentType: "application/x-www-form-urlencoded",
                sync:false,
                data: {
                    password: $("#pass-form [name='password']").val()
                },
                async: true,                // 异步执行
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success("验证通过！");
                        js.confirmPost({
                            content: "尊敬的用户，重置并撤销任务会清空本任务的所有数据（包括审核数据），您确认要重置并撤销任务吗？",
                            url: ctx + 'getData/restTask',
                            type: 'post',
                            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                            dataType: 'json',
                            contentType: "application/x-www-form-urlencoded",
                            data: {
                                id: $("#btn-verification").attr("data")
                            },
                            async: true,                // 异步执行
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    js.modal.success(result.msg);
                                    js.modal.hide("modal-pass");
                                    js.table.refresh(table);
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            },
                            error: function () {
                                js.modal.fail();
                            }
                        });
                    }else{
                        js.modal.warning("验证失败！无法重置并撤销任务！");
                    }
                },
                error: function () {
                    js.modal.fail();
                }
            });
        });
    });

    /**
     * 创建成本清单
     */
    function createCostSheet(code) {
        js.post({
            url: ctx + 'getData/checkIsJump',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                code: code
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.confirmPost({
                        content: "尊敬的用户，您是否创建核算成本清单？",
                        url: ctx + 'getData/createCostSheet',
                        type: 'post',
                        traditional: true,            // 阻止深度序列化， 使参数可以使用数组
                        dataType: 'json',
                        contentType: "application/x-www-form-urlencoded",
                        data: {
                            proCode: code
                        },
                        async: true,                // 异步执行
                        success: function (result) {
                            if (result.type === web_status.SUCCESS) {
                                js.modal.success(result.msg);
                                js.table.refresh(table);
                            } else {
                                js.modal.warning(result.msg);
                            }
                        },
                        error: function () {
                            js.modal.fail();
                        }
                    });
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }

    /**
     * 插销创建成本清单
     */
    function revoke(batId) {
        js.confirmPost({
            content: "尊敬的用户，您是否创撤销创建核算成本清单？",
            url: ctx + 'getData/revoke',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                batId: batId
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });

    }

    /**
     * 是否取消保存
     **/
    function cancelSave(id) {
        js.confirmPost({
            content: "尊敬的用户，您确认要取消保存核算成本清单吗？",
            url: ctx + 'getData/cancelSave',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                id: id
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    window.parent.getItemCount();
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }
    /**
     * 跳转到数据获取页面
     * @param adtId
     */
    function gotoGetData(adtId, code) {
        js.post({
            url: ctx + 'getData/checkIsJump',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                batId: adtId,
                code: code
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    location.href = ctx + 'getData/accounting/' + adtId + "/" + code;
                } else {
                    js.modal.warning("产品结构不存在！无法获取数据！");
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }
    /**
     * 跳转到成本核算页面
     * @param adtId
     */
    function gotoAccounting(adtId, code) {
        js.post({
            url: ctx + 'getData/checkIsJump',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                code: code
            },
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    location.href = ctx + 'getData/accountingCostEdit/' + adtId + "/" + code;
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
    }

    /**
     * 时间格式化
     * @param time 时间
     * @param format 格式
     * @returns 格式化之后的时间
     */
    function formatDate(time, format = 'YY-MM-DD hh:mm:ss') {
        let date = new Date(time);
        let year = date.getFullYear(),
            month = date.getMonth() + 1,//月份是从0开始的
            day = date.getDate(),
            hour = date.getHours(),
            min = date.getMinutes(),
            sec = date.getSeconds();
        let preArr = Array.apply(null, Array(10)).map(function (elem, index) {
            return '0' + index;
        });//开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]
        let newTime = format.replace(/YY/g, year)
            .replace(/MM/g, preArr[month] || month)
            .replace(/DD/g, preArr[day] || day)
            .replace(/hh/g, preArr[hour] || hour)
            .replace(/mm/g, preArr[min] || min)
            .replace(/ss/g, preArr[sec] || sec);
        return newTime;
    }

    /**
     * 工作组下拉框改变时修改搜索使用隐藏域的值
     * @param obj
     */
    function changeGroupNameStrVal(obj) {
        $("#queryForm [name='groupNameStr']").val($(obj).val());
    }

    /**
     * 更换责任人模态框
     * @param batId
     */
    function change(batId,userName,groupId){
        $("#currentFounder").text(userName);
        $("#batId").val(batId);
        js.post({
            url: ctx + 'getData/getNewUserList',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            sync:false,
            data: {
                userName:userName
            },
            async: true,                // 异步执行
            success: function (result) {
                $("#newFounder").empty();
                $("#newFounder").append('<option>-- 请选择创建人 --</option>');
                let data = result.data.rows;
                for (let i = 0; i < data.length; i++) {
                    $("#newFounder").append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
                }
                js.modal.open('modal-change');
            },
            error: function () {
                js.modal.fail();
            }
        });

    }

    /**
     * 重置并撤销任务
     * @param batId
     */
    function restTask(batId){
        $("#btn-verification").attr("data",parseInt(batId));
        js.modal.open("modal-pass");


    }
</script>
</body>
</html>